#ifndef CSERVER_DB_H_
#define CSERVER_DB_H_
#include <cserver/configfile.h>
#include <cserver/data.h>
#include <cserver/protocol.h>
#include <memory>

namespace csrv{
	struct Environment{
		int16_t temperature, humidity, pm25,pm10,pm1,wind_speed,wind_direction,noise;
	};

	class Context;
	class DatabaseImp;
	class Database{
		std::unique_ptr<DatabaseImp> m_imp;
		public:

		explicit Database(Context&);
		~Database();

		void forwardData(const std::string& euid, std::function<void(const std::string&, const std::string&)> sender); 
		void saveDeviceRecord(const app_t&);
		void saverHhydrant(const std::string& ip_addr_port, const std::string& euid, int32_t pressure, int32_t trend, uint16_t signal, const std::string& sim_id, int32_t voltage);
		void saveSoak(const std::string& ip_addr_port, const std::string& euid, int32_t soak, int32_t trend, uint16_t signal, const std::string& sim_id, int32_t voltage);
		void saveWellLip(const std::string& ip_addr_port, const std::string& euid, uint16_t signal, const std::string& sim_id, uint16_t cover_status, uint16_t submerged);
		void saveEnvironment(const std::string& ip_addr_port, const std::string& euid, uint16_t signal, const std::string& sim_id, uint16_t interval, const std::vector<Environment>& env_data,
				std::function<void(uint16_t )> sender);

		void saveFiredoor(const std::string& ip_addr_port, const std::string& euid, uint16_t signal, const std::string& sim_id, int32_t voltage, uint16_t reed_status, uint16_t submerged);
		void saverSprinkler(const std::string& ip_addr_port, const std::string& euid, int32_t pressure, int32_t voltage, uint16_t signal, const std::string& sim_id);
		void saverFirebox(const std::string& ip_addr_port, const std::string& euid, int32_t water_deepth, int32_t voltage, uint16_t signal, const std::string& sim_id);
		void voiceHeartbeat(const std::string& ip_addr_port, const std::string& euid, uint16_t signal, const std::string& sim_id, uint16_t voice, std::function<void(uint8_t )> sender);
		void saverFirePump(const std::string& ip_addr_port, const std::string& euid, int32_t pressure, int32_t voltage, uint16_t signal, const std::string& sim_id);
		void saverWaterQuality(const std::string& ip_addr_port, const std::string& euid, uint16_t signal, const std::string& sim_id
				, uint32_t temperature, uint32_t ntu, float ph, float orp, float ec, float nh4, uint32_t do_value, int16_t mover,  float latitude, float longitude, uint32_t uvt);
		void clear_device_init_flag(const std::string& euid);
		void clear_device_param_flag(const std::string& euid);
		void save_electric(const std::string& ip_addr_port, const pcm_350a_data& data, std::function<void(const std::vector<char>&)> sender);

		void save_anolog(const std::string& ip_addr_port, const xhs_send_anolog& data, std::function<void(const std::string&)> sender);
		void save_anolog_config(const std::string& ip_addr_port, const xhs_send_anolog_config& data, std::function<void(const std::string&)> sender);
		bool save_fire_protection(const fire_package& pkg, std::function<void(const std::vector<char>&)> sender);
		void update_sim_id(const std::string& euid, const std::string& simid, const std::string& ipaddr);
		void save_fire_pump_and_fan_info(const info_fire_pump_information& data);
		void save_waterater_data(const info_waterater_data& data);
		void save_sponge_data(const sponge_data& data, std::function<void(const std::vector<char>&)> sender);
		void save_sponge_upconfig(const sponge_upconfig& data, std::function<void(const std::vector<char>&)> sender);
	
		void writeTest(const std::string& str);
		void sendHydrantSMS();

		void startWorker();
		bool workerIsRunning() const;
		void stopWorker();
	};
	using DatabasePtr = std::shared_ptr<Database>;
	
}
#endif //end CSERVER_DB_H_
